﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Copad.BusinessCard.Model;
using Copad.BusinessCard.BLL;
using System.Diagnostics;
using System.Configuration;

public partial class CardList : BusinessCardPageBase
{
    CardBLL cardBLL = new CardBLL();
    protected List<CardInfo> CardInfoList;
    protected Card CurrentCard;
    protected bool IsFavorite = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        string mode = Request["mode"];
        this.hidCategoryID.Value = Request["id"];

        if (!IsPostBack)
        {
            HttpCookie searchAll = Request.Cookies["SearchAll"];
            if (searchAll != null && searchAll.Value == "true")
            {
                this.chkSearchAll.Checked = true;
            }
            searchAll = Request.Cookies["SortOrder"];
            if (searchAll != null && searchAll.Value == "0")
            {
                this.sortOrder.SelectedValue = "0";
                cardBLL.OrderBy = "name";
            }
            else
            {
                this.sortOrder.SelectedValue = "1";
                cardBLL.OrderBy = "CreateTime";
            }
        }

        if (string.IsNullOrEmpty(mode))
        {

            LoadAllCard();
        }
        else if (mode == "favorite")
        {
            IsFavorite = true;
            LoadFavoriteCard();
        }
    }

    private void LoadAllCard()
    {
        string ids = Request["id"];
        if (string.IsNullOrEmpty(ids))
        {
            ids = "0";
        }
        long id = 0;
        if (!long.TryParse(ids, out id))
        {
            CardInfoList = new List<CardInfo>(0);
            return;
        }

        //Stopwatch watch = new Stopwatch();
        //watch.Start();
        if (id == 0)
        {
            CardInfoList = cardBLL.GetUngroupedCardInfo(Manager.CurrentUser.User.ID);
        }
        else
        {
            CardInfoList = cardBLL.GetCardInfoByCID(id, Manager.CurrentUser.User.ID);
        }
        //watch.Stop();
        //Response.Write("分类获取时间：" + watch.ElapsedMilliseconds);
        //watch.Start();
        if (CardInfoList.Count > 0)
        {
            CurrentCard = cardBLL.Get(CardInfoList[0], Manager.CurrentUser.User.ID);
        }
        else
        {
            CurrentCard = new Card { Summary = new CardInfo() };
        }
        CurrentCard.InitNumbers();
        //watch.Stop();
        //Response.Write("名片获取时间：" + watch.ElapsedMilliseconds);
    }

    private void LoadFavoriteCard()
    {
        CardInfoList = cardBLL.GetFavoriteCard(Manager.CurrentUser.User.ID);
        if (CardInfoList.Count > 0)
        {
            CurrentCard = cardBLL.Get(CardInfoList[0], Manager.CurrentUser.User.ID);
        }
        else
        {
            CurrentCard = new Card { Summary = new CardInfo() };
        }
        CurrentCard.InitNumbers();
    }
}